home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr05 / mswlogo3.zip / MSWLOGO.ZIP / EXAMPLES.ZIP / HANOI < prev    next >
Text File  |  1992-12-03  |  3KB  |  167 lines

  1. to hanoi :number 
  2. ;
  3. ; Towers of Hanoi
  4. ; Meyer A. Billmers
  5. ; November, 1983
  6. ;
  7. ; This procedure plays a graphic version of the Towers of Hanoi puzzle
  8. ; The argument is the number of disks in the configuration.
  9. ;
  10. ; c.f. putdisk, towercnt,towerset, hanoihlpr
  11. local "from
  12. local "to
  13. local "other
  14. local "datfil
  15. ;make "datfil openw "hanoi.dat
  16. ;fileprint :datfil (sentence [Hanoi of ] :number [towers Started at: ] time)
  17. ; to change the starting and ending needles, change the next three assignments
  18. make "from 1
  19. make "to 3
  20. make "other 2
  21. cs
  22. ht
  23. penpaint
  24. setpensize [5 5]
  25. ; first we draw the table and the golden needles
  26. setpencolor 255 0 0
  27. pu
  28. setxy -350 -100
  29. pd
  30. setxy 350 -100
  31. pu
  32. setx -240
  33. pd
  34. fd 250
  35. pu
  36. setxy -15 -100
  37. pd
  38. fd 250
  39. pu
  40. setxy 210 -100
  41. pd
  42. fd 250
  43. make "tower1 0
  44. make "tower2 0
  45. make "tower3 0
  46. ; draw the initial stack of disks. note that putdisk draws the 
  47. ; "fixed up" towers.
  48. repeat :number ~
  49.    [~
  50.    putdisk :from :number - repcount + 1 "final ~
  51.    ifelse :from = 1 ~
  52.       [make "tower1 :tower1 + 1] ~
  53.       [ifelse :from = 2 ~
  54.          [make "tower2 :tower2 + 1] ~
  55.          [make "tower3 :tower3 + 1] ~
  56.       ] ~
  57.    ]
  58. hanoihlpr :number :from :to :other
  59. ; fileprint :datfil (sentence [Hanoi Ended at: ] time)
  60. ; close :datfil
  61. end
  62.  
  63. to hanoihlpr :number :from :to :other
  64. ;
  65. ; Called by HANOI. Contains the actual recursive Towers of Hanoi algorithm
  66. ;
  67. local "tcf 
  68. local "tct
  69. if equalp :number 0 [stop]
  70. hanoihlpr :number-1 :from :other :to
  71. make "tcf towercnt :from
  72. make "tct towercnt :to
  73. towerset :from :tcf - 1
  74. putdisk :from :number "temp
  75. putdisk :to :number "temp
  76. putdisk :from :number "erase
  77. putdisk :to :number "final
  78. towerset :to :tct + 1
  79. hanoihlpr :number-1 :other :to :from
  80. end
  81.  
  82. to putdisk :tnum :dnum :state
  83. ;
  84. ; Called by HANOI to put a disk on a tower.
  85. ; first arg. is number of tower (1,2 or 3)
  86. ; second arg. is number of disk to draw (1 is smallest)
  87. ; third arg. is "final, "temp, or "erase depending on whether
  88. ;   disk is drawn in final state, in temporary state to indicate
  89. ;   motion, or is being erased (removed from this tower)
  90. ; Note that this procedure re-draws the tower correctly.
  91. ;
  92. local "tc
  93. local "halfsize
  94. make "tc towercnt :tnum
  95. make "halfsize sum 20 product :dnum 10
  96. pu
  97. ifelse :tnum = 1 ~
  98.    [setxy "-240 "-100] ~
  99.    [ ~
  100.    ifelse :tnum = 2 ~
  101.       [setxy "-15 "-100] ~
  102.       [setxy 210 "-100] ~
  103.    ]
  104. pe
  105. fd product 30 :tc
  106. pu
  107. setxy xcor - :halfsize ycor
  108. pd
  109. penpaint
  110. ifelse :state = "final ~
  111.    [setpencolor 0 255 0] ~
  112.    [ ~
  113.    ifelse :state = "temp ~
  114.       [setpencolor 0 0 255] ~
  115.       [pe] ~
  116.    ]
  117. fd 30
  118. rt 90
  119. fd product :halfsize 2
  120. rt 90
  121. fd 30
  122. rt 90
  123. pu
  124. fd :halfsize
  125. rt 90
  126. setpencolor 255 0 0
  127. ifelse :state = "erase ~
  128.    [ ~
  129.    pd ~
  130.    penpaint ~
  131.    fd 30 ~
  132.    ] ~
  133.    [ ~
  134.    pe ~
  135.    fd 30 ~
  136.    ]
  137. end
  138.  
  139. to towercnt :tn
  140. ;
  141. ; Called by HANOI. Returns the current number of disks on tower :tn,
  142. ; as stored in the globals tower1, tower2, and tower3.
  143. ;
  144. ifelse :tn = 1 ~
  145.    [output :tower1] ~
  146.    [ ~
  147.    ifelse :tn = 2 ~
  148.       [output :tower2] ~
  149.       [output :tower3] ~
  150.    ]
  151. end
  152.  
  153. to towerset :tn :value
  154. ;
  155. ; Called by HANOI. Sets the current number of disks on tower :tn,
  156. ; as stored in the globals tower1, tower2, and tower3.
  157. ;
  158. ifelse :tn = 1 ~
  159.    [make "tower1 :value] ~
  160.    [ ~
  161.    ifelse :tn = 2 ~
  162.       [make "tower2 :value] ~
  163.       [make "tower3 :value] ~
  164.    ]
  165. end
  166.